<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="author" content="李愧愧">
    <title></title>
    <style>
        .box {
            width: 141px;
            height: 406px;
            border: 1px solid black;
            margin: 0 auto;
            box-sizing: border-box;
        }

        .boxTop {
            width: 120px;
            height: 30px;
            margin: 0 auto;
            text-align: center;
            border-bottom: 1px solid #000;
            padding: 5px;
        }

        .boxTop input {
            /* margin-top: 10px;
            margin-bottom: 10px; */
        }

        .boxBom form {
            padding: 30px;
            padding-top: 5px;
        }

        .boxBom form input {
            margin-top: 10px;

        }
    </style>
</head>

<body>
    <div class="box">
        <div class="boxTop">
            <input type="checkbox" name="" id="">
            <label for="" class=".ab">全选</label>
            <span href="">反选</span>
        </div>
        <div class="boxBom">
            <form action="">
                <input type="checkbox" name="" id="">选项1
                <input type="checkbox" name="" id="">选项2
                <input type="checkbox" name="" id="">选项3
                <input type="checkbox" name="" id="">选项4
                <input type="checkbox" name="" id="">选项5
                <input type="checkbox" name="" id="">选项6
                <input type="checkbox" name="" id="">选项7
                <input type="checkbox" name="" id="">选项8
                <input type="checkbox" name="" id="">选项9
                <input type="checkbox" name="" id="">选项10
                <input type="checkbox" name="" id="">选项11
                <input type="checkbox" name="" id="">选项12
            </form>
        </div>
    </div>

    <script>
        var xuanze = document.querySelector('.boxTop input');
        var input = document.querySelectorAll('.boxBom form input');
        var a = document.querySelector('.boxTop span');
        var label = document.querySelector('label');
        // console.log(label.innerHTML);

        xuanze.onclick = function () {
            for (var i = 0; i < input.length; i++) {
                input[i].checked = xuanze.checked;

            }
            isClick();
        }

        // 反选
        a.onclick = function () {
            for (var i = 0; i < input.length; i++) {
                console.log(input[i].checked);
                input[i].checked = !input[i].checked;
                console.log(input[i].checked);
            }
            isClick();

        }

        // 判断全选状态
        for (var j = 0; j < input.length; j++) {
            input[j].onclick = function () {
                isClick();
            }
        }

        function isClick(){
            var clickAll = true;
            for(var i = 0; i < input.length; i++){
                if(input[i].checked == false){
                    clickAll = false;
                    break;
                }
            }
            xuanze.checked = clickAll;

            if(clickAll){
                label.innerHTML = "全不选";
            }else{
                label.innerHTML = '全选';
            }

        }
        
    </script>
</body>

</html>